package com.hefei.blog.base.utils.image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

public class AbstractMageUtils {

	public static void validate(File ori, String dest) throws IOException {
		File destFile = new File(dest);
		
		if (ori == null) {
            throw new NullPointerException("Source must not be null");
        }
        if (dest == null) {
            throw new NullPointerException("Destination must not be null");
        }
        if (ori.exists() == false) {
            throw new FileNotFoundException("Source '" + ori + "' does not exist");
        }
        if (ori.isDirectory()) {
            throw new IOException("Source '" + ori + "' exists but is a directory");
        }
        if (ori.getCanonicalPath().equals(destFile.getCanonicalPath())) {
            throw new IOException("Source '" + ori + "' and destination '" + dest + "' are the same");
        }
		if (destFile.getParentFile() != null && destFile.getParentFile().exists() == false) {
            if (destFile.getParentFile().mkdirs() == false) {
                throw new IOException("Destination '" + dest + "' directory cannot be created");
            }
        }
        if (destFile.exists() && destFile.canWrite() == false) {
            throw new IOException("Destination '" + dest + "' exists but is read-only");
        }
	}
	
	public static int[] getSize(String dest) throws IOException {
		File destFile = new File(dest);
		BufferedImage src = ImageIO.read(destFile); // 读入文件
		int w = src.getWidth();
		int h = src.getHeight();
		return new int[]{w, h};
	}
}
